
*12345678901234567890123456789012345678901234567890123456789012345678901234567890
capture log close
clear all
set more off

*	************************************************************************
* 	File-Name: 	AccessOffGridCoding.do
*	Log-file:	na
*	Date:  		07/20/2019
*	Author: 	Michaël Aklin and Johannes Urpelainen
*	Data Used:  various
*	Output		na
*	Purpose:   	.do file for the coding of off-grid/ACCESS-2 project
*	************************************************************************

*	************************************************************************
* 	1. Data
*	************************************************************************

use "PATH_TO_RAW_ACCESS_PANEL_DATASET", clear

*	************************************************************************
*	2. New variables
*	************************************************************************

* Time
destring year, replace
gen wave = .
replace wave = 1 if year == 2015
replace wave = 2 if year == 2018

* Cross section
encode finalhhid, gen(hhid)
encode m1_q9_district, gen(districtid)

* xtset
xtset hhid wave

* Panel flag
gen panel_flag = .
bysort hhid (wave): replace panel_flag = 0 if _N==1
bysort hhid (wave): replace panel_flag = 1 if _N==2


* Covariates
gen logexp = log(m1_q32_month_expenditure+1)
gen female = 0
replace female = 1 if m1_q20_gender == 0

* Outcomes

gen dm2_q55_grid = s.m2_q55_grid
gen fdm2_q55_grid = f.dm2_q55_grid

gen dm2_q58_micro_use = s.m2_q58_micro_use

gen microg_loss = .
replace microg_loss = 0 if dm2_q58_micro_use != .
replace microg_loss = 1 if dm2_q58_micro_use == -1
replace microg_loss = . if l.m2_q58_micro_use == 0

gen microg_gain = .
replace microg_gain = 0 if dm2_q58_micro_use != .
replace microg_gain = 1 if dm2_q58_micro_use == 1
replace microg_gain = . if l.m2_q58_micro_use == 1

gen fmicrog_loss = f.microg_loss
gen fmicrog_gain = f.microg_gain
gen fdm2_q58_micro_use = f.dm2_q58_micro_use
gen fm2_q55_grid = f.m2_q55_grid

* Version based on other question
gen micro_v2 = .
replace micro_v2 = 0 if m3_q83_light_main != .
replace micro_v2 = 1 if m3_q83_light_main == 3

gen dmicro_v2 = d.micro_v2

gen microg_loss_v2 = .
replace microg_loss_v2 = 0 if dmicro_v2 != .
replace microg_loss_v2 = 1 if dmicro_v2 == -1
replace microg_loss_v2 = . if l.micro_v2 == 0

gen microg_gain_v2 = .
replace microg_gain_v2 = 0 if dmicro_v2 != .
replace microg_gain_v2 = 1 if dmicro_v2 == 1
replace microg_gain_v2 = . if l.micro_v2 == 1

gen fdmicro_v2 = f.dmicro_v2
gen fmicrog_gain_v2 = f.microg_gain_v2
gen fmicrog_loss_v2 = f.microg_loss_v2



* Solar home systems/lanterns
gen shslanterns_v2 = .
replace shslanterns_v2 = 0 if m3_q83_light_main != .
replace shslanterns_v2 = 1 if m3_q83_light_main == 4

gen dshslanterns_v2 = d.shslanterns_v2

gen shslanterns_loss_v2 = .
replace shslanterns_loss_v2 = 0 if dshslanterns_v2 != .
replace shslanterns_loss_v2 = 1 if dshslanterns_v2 == -1
replace shslanterns_loss_v2 = . if l.shslanterns_v2 == 0

gen shslanterns_gain_v2 = .
replace shslanterns_gain_v2 = 0 if dshslanterns_v2 != .
replace shslanterns_gain_v2 = 1 if dshslanterns_v2 == 1
replace shslanterns_gain_v2 = . if l.shslanterns_v2 == 1

gen fdshslanterns_v2 = f.dshslanterns_v2
gen fshslanterns_loss_v2 = f.shslanterns_loss_v2
gen fshslanterns_gain_v2 = f.shslanterns_gain_v2

* Ownership and primary usage
gen own_grid = .
	replace own_grid = 0 if m2_q55_grid == 0
	replace own_grid = 1 if m2_q55_grid == 1
gen own_shs = .
	replace own_shs = 0 if m2_q60_solar_use == 4 | m2_q60_solar_use == 2 | m2_q59_solar_aware == 0
	replace own_shs = 1 if m2_q60_solar_use == 1 | m2_q60_solar_use == 3
gen own_dieselmg = .
	replace own_dieselmg = 0 if m2_q58_1_micro_source != 2
	replace own_dieselmg = 1 if m2_q58_1_micro_source == 2
gen own_solarmg = .
	replace own_solarmg = 0 if m2_q58_1_micro_source != 1
	replace own_solarmg = 1 if m2_q58_1_micro_source == 1
gen own_solarlantern = .
	replace own_solarlantern = 0 if m2_q60_solar_use == 4 | m2_q60_solar_use == 1 | m2_q59_solar_aware == 0
	replace own_solarlantern = 1 if m2_q60_solar_use == 2 | m2_q60_solar_use == 3
gen own_kerosenelantern = .
	replace own_kerosenelantern = 0 if m2_q61_kerolamp_use == 0
	replace own_kerosenelantern = 1 if m2_q61_kerolamp_use == 1

gen primary_grid = 0
	replace primary_grid = 1 if m3_q83_light_main == 1 & own_grid == 1
gen primary_shs = 0 
	replace primary_shs = 1 if m3_q83_light_main == 4 & own_shs == 1
gen primary_dieselmg = 0
	replace primary_dieselmg = 1 if m3_q83_light_main == 3 & own_dieselmg == 1
gen primary_solarmg = 0
	replace primary_solarmg = 1 if m3_q83_light_main == 3 & own_solarmg == 1
gen primary_solarlantern = 0 
	replace primary_solarlantern = 1 if m3_q83_light_main == 4 & own_solarlantern == 1 & own_shs != 1 // Assumption: SHS owners use SHS as primary source of lighting
gen primary_kerosenelantern = 0
	replace primary_kerosenelantern = 1 if m3_q83_light_main == 2 & own_kerosenelantern == 1

gen overall_primary = "Grid" if primary_grid == 1
replace overall_primary = "SHS" if primary_shs == 1
replace overall_primary = "Diesel MG" if primary_dieselmg == 1
replace overall_primary = "Solar MG" if primary_solarmg == 1
replace overall_primary = "Solar lantern" if primary_solarlantern == 1
replace overall_primary = "Kerosene lantern" if primary_kerosenelantern == 1


gen fdown_grid = f.d.own_grid
gen fdown_shs = f.d.own_shs
gen fdown_dieselmg = f.d.own_dieselmg 
gen fdown_solarmg = f.d.own_solarmg
gen fdown_solarlantern = f.d.own_solarlantern
gen fdown_kerosenelantern = f.d.own_kerosenelantern
gen fdprimary_grid = f.d.primary_grid
gen fdprimary_shs = f.d.primary_shs
gen fdprimary_dieselmg = f.d.primary_dieselmg
gen fdprimary_solarmg = f.d.primary_solarmg
gen fdprimary_solarlantern = f.d.primary_solarlantern
gen fdprimary_kerosenelantern = f.d.primary_kerosenelantern

gen fgain_own_grid = .
	replace fgain_own_grid = 0 if own_grid == 0 & f.own_grid == 0
	replace fgain_own_grid = 1 if own_grid == 0 & f.own_grid == 1
gen fgain_own_shs = .
	replace fgain_own_shs = 0 if own_shs == 0 & f.own_shs == 0
	replace fgain_own_shs = 1 if own_shs == 0 & f.own_shs == 1
gen fgain_own_dieselmg = .
	replace fgain_own_dieselmg = 0 if own_dieselmg == 0 & f.own_dieselmg == 0
	replace fgain_own_dieselmg = 1 if own_dieselmg == 0 & f.own_dieselmg == 1
gen fgain_own_solarmg = .
	replace fgain_own_solarmg = 0 if own_solarmg == 0 & f.own_solarmg == 0
	replace fgain_own_solarmg = 1 if own_solarmg == 0 & f.own_solarmg == 1	
gen fgain_own_solarlantern = .
	replace fgain_own_solarlantern = 0 if own_solarlantern == 0 & f.own_solarlantern == 0
	replace fgain_own_solarlantern = 1 if own_solarlantern == 0 & f.own_solarlantern == 1
gen fgain_own_kerosenelantern = .
	replace fgain_own_kerosenelantern = 0 if own_kerosenelantern == 0 & f.own_kerosenelantern == 0
	replace fgain_own_kerosenelantern = 1 if own_kerosenelantern == 0 & f.own_kerosenelantern == 1
gen fgain_primary_grid = .
	replace fgain_primary_grid = 0 if primary_grid == 0 & f.primary_grid == 0
	replace fgain_primary_grid = 1 if primary_grid == 0 & f.primary_grid == 1
gen fgain_primary_shs = .
	replace fgain_primary_shs = 0 if primary_shs == 0 & f.primary_shs == 0
	replace fgain_primary_shs = 1 if primary_shs == 0 & f.primary_shs == 1
gen fgain_primary_dieselmg = .
	replace fgain_primary_dieselmg = 0 if primary_dieselmg == 0 & f.primary_dieselmg == 0
	replace fgain_primary_dieselmg = 1 if primary_dieselmg == 0 & f.primary_dieselmg == 1
gen fgain_primary_solarmg = .
	replace fgain_primary_solarmg = 0 if primary_solarmg == 0 & f.primary_solarmg == 0
	replace fgain_primary_solarmg = 1 if primary_solarmg == 0 & f.primary_solarmg == 1
gen fgain_primary_solarlantern = .
	replace fgain_primary_solarlantern = 0 if primary_solarlantern == 0 & f.primary_solarlantern == 0
	replace fgain_primary_solarlantern = 1 if primary_solarlantern == 0 & f.primary_solarlantern == 1
gen fgain_primary_kerosenelantern = .
	replace fgain_primary_kerosenelantern = 0 if primary_kerosenelantern == 0 & f.primary_kerosenelantern == 0
	replace fgain_primary_kerosenelantern = 1 if primary_kerosenelantern == 0 & f.primary_kerosenelantern == 1

gen floss_own_grid = .
	replace floss_own_grid = 0 if own_grid == 1 & f.own_grid == 1
	replace floss_own_grid = 1 if own_grid == 1 & f.own_grid == 0
gen floss_own_shs = .
	replace floss_own_shs = 0 if own_shs == 1 & f.own_shs == 1
	replace floss_own_shs = 1 if own_shs == 1 & f.own_shs == 0
gen floss_own_dieselmg = .
	replace floss_own_dieselmg = 0 if own_dieselmg == 1 & f.own_dieselmg == 1
	replace floss_own_dieselmg = 1 if own_dieselmg == 1 & f.own_dieselmg == 0
gen floss_own_solarmg = .
	replace floss_own_solarmg = 0 if own_solarmg == 1 & f.own_solarmg == 1
	replace floss_own_solarmg = 1 if own_solarmg == 1 & f.own_solarmg == 0	
gen floss_own_solarlantern = .
	replace floss_own_solarlantern = 0 if own_solarlantern == 1 & f.own_solarlantern == 1
	replace floss_own_solarlantern = 1 if own_solarlantern == 1 & f.own_solarlantern == 0
gen floss_own_kerosenelantern = .
	replace floss_own_kerosenelantern = 0 if own_kerosenelantern == 1 & f.own_kerosenelantern == 1
	replace floss_own_kerosenelantern = 1 if own_kerosenelantern == 1 & f.own_kerosenelantern == 0
gen floss_primary_grid = .
	replace floss_primary_grid = 0 if primary_grid == 1 & f.primary_grid == 1
	replace floss_primary_grid = 1 if primary_grid == 1 & f.primary_grid == 0
gen floss_primary_shs = .
	replace floss_primary_shs = 0 if primary_shs == 1 & f.primary_shs == 1
	replace floss_primary_shs = 1 if primary_shs == 1 & f.primary_shs == 0
gen floss_primary_dieselmg = .
	replace floss_primary_dieselmg = 0 if primary_dieselmg == 1 & f.primary_dieselmg == 1
	replace floss_primary_dieselmg = 1 if primary_dieselmg == 1 & f.primary_dieselmg == 0
gen floss_primary_solarmg = .
	replace floss_primary_solarmg = 0 if primary_solarmg == 1 & f.primary_solarmg == 1
	replace floss_primary_solarmg = 1 if primary_solarmg == 1 & f.primary_solarmg == 0
gen floss_primary_solarlantern = .
	replace floss_primary_solarlantern = 0 if primary_solarlantern == 1 & f.primary_solarlantern == 1
	replace floss_primary_solarlantern = 1 if primary_solarlantern == 1 & f.primary_solarlantern == 0
gen floss_primary_kerosenelantern = .
	replace floss_primary_kerosenelantern = 0 if primary_kerosenelantern == 1 & f.primary_kerosenelantern == 1
	replace floss_primary_kerosenelantern = 1 if primary_kerosenelantern == 1 & f.primary_kerosenelantern == 0

gen saubhagya = .
	replace saubhagya = 0 if l.own_grid == 0
	replace saubhagya = 0 if m6_5n4_saubhagya_electrified == 0
	replace saubhagya = 1 if m6_5n4_saubhagya_electrified == 1
	
gen lighting_group = ""
replace lighting_group = "Grid + Solar lantern" if own_grid == 1 & own_shs == 0 & own_solarlantern == 1 & own_dieselmg == 0 & own_solarmg == 0  & own_kerosenelantern == 0
replace lighting_group = "Grid + SHS" if own_grid == 1 & own_shs == 1 & own_solarlantern == 0 & own_dieselmg == 0 & own_solarmg == 0 & own_kerosenelantern == 0
replace lighting_group = "Grid" if own_grid == 1 & own_shs == 0 & own_solarlantern == 0 & own_dieselmg == 0 & own_solarmg == 0  & own_kerosenelantern == 0
replace lighting_group = "Solar lantern" if own_grid == 0 & own_shs == 0 & own_solarlantern == 1 & own_dieselmg == 0 & own_solarmg == 0  & own_kerosenelantern == 0
replace lighting_group = "SHS" if own_grid == 0 & own_shs == 1 & own_solarlantern == 0 & own_dieselmg == 0 & own_solarmg == 0  & own_kerosenelantern == 0
replace lighting_group = "Other combo" if lighting_group == ""

gen dist_lighting_group = ""
replace dist_lighting_group = "Grid" 					if own_grid == 1 & own_shs == 0 & own_solarlantern == 0
replace dist_lighting_group = "Grid + Solar lantern" 	if own_grid == 1 & own_shs == 0 & own_solarlantern == 1 
replace dist_lighting_group = "Grid + SHS" 				if own_grid == 1 & own_shs == 1 & own_solarlantern == 0 
replace dist_lighting_group = "Solar lantern" 			if own_grid == 0 & own_shs == 0 & own_solarlantern == 1 
replace dist_lighting_group = "SHS" 					if own_grid == 0 & own_shs == 1 & own_solarlantern == 0 
replace dist_lighting_group = "Other combo" if dist_lighting_group == ""


label var own_grid "Have grid"
label var own_shs "Have solar home systems"
label var own_dieselmg "Have diesel migrogrid"
label var own_solarmg "Have solar microgrid"
label var own_solarlantern "Have solar lantern"
label var own_kerosenelantern "Have kerosene lantern"

label var primary_grid "Primary source of light: grid"
label var primary_shs "Primary source of light: solar home system"
label var primary_dieselmg "Primary source of light: diesel microgrid"
label var primary_solarmg "Primary source of light: solar microgrid"
label var primary_solarlantern "Primary source of light: solar lantern"
label var primary_kerosenelantern "Primary source of light: kerosene lantern"

label var fdown_grid "$\Delta$ Grid"
label var saubhagya "Saubhagya ($=$1)"

* Village-level data
bysort m1_q11_village_code wave: egen mean_grid = mean(m2_q55_grid)
xtset hhid wave
gen mean_grid_wave1 = mean_grid if wave==1
replace mean_grid_wave1 = l.mean_grid_wave1 if wave==2

bysort m1_q11_village_code wave: egen mean_hours = mean(m2_q69_elec_hrs)
bysort m1_q11_village_code wave: egen mean_saubhagya = mean(saubhagya)
bysort m1_q11_village_code wave: egen min_saubhagya = min(saubhagya)
bysort m1_q11_village_code wave: egen max_saubhagya = max(saubhagya)

xtset hhid wave
gen dmean_hours = s.mean_hours

label var mean_grid "Mean grid elec (village-level)"
label var mean_hours "Mean hours of elec (village-level)"
label var mean_saubhagya "Mean Saubhagya (village-level)"
label var min_saubhagya "Saubhagya (all) (village-level)"
label var max_saubhagya "Saughabya (at least 1) (village-level)"
label var dmean_hours "$\Delta$ Hours"

*	************************************************************************
* 	3. Census data
*	************************************************************************

merge m:1 m1_q11_village_code using "./Data/Datasets/Census2011/Census2011_clean.dta"
drop _merge
saveold "./Data/DataCleaned.dta", replace v(12)


*	************************************************************************
* 	4. Distance to town data
*	************************************************************************

clear all
import delimited "./Data/Datasets/Census2011/district.csv"

duplicates drop c11_2011_vill_code, force
rename c11_2011_vill_code m1_q11_village_code

merge 1:m m1_q11_village_code using "./Data/DataCleaned.dta"
drop if _merge==1
drop _merge

destring c01_2001_dist_town, replace force
destring c11_2011_total_pop, replace
destring c11_2011_pucca_road, replace

gen logpop = log(c11_2011_total_pop)

*	************************************************************************
* 	5. Labels
*	************************************************************************

label var logexp "Expenditure (log)"
label var female "Female"
label var wave "Wave"
label var fm2_q55_grid "Grid at time t"
label var dm2_q55_grid "$\Delta$ Grid"
label var fdm2_q55_grid "$\Delta$ Grid"
label var c01_2001_dist_town "Distance to town"
label var logpop "Population (log)"
label var c11_2011_pucca_road "Pucca road"


*	************************************************************************
*	6. Save
*	************************************************************************

saveold "DataCleaned.dta", replace v(12)


